namespace EFS.EventLog.Config
{
	/// <summary>
	/// Log filter
	/// </summary>
	public class EventsFilter
	{ 
		public System.Boolean Info = true;
		public System.Boolean Warn { get; private set; }
		public System.Boolean Err { get; private set; }
		public System.Boolean Except { get; private set; }
		public System.Boolean Extended { get; private set; }
		public System.Boolean Counter { get; private set; }

		/// <summary>
		/// Check if any 
		/// </summary>
		public System.Boolean Any
		{
			get 
			{
				return Info || Warn || Err || Except || Extended;
			}
		}

		/// Load filter from web.config		
		internal void LoadConfig(System.String destination)
		{
			// make path
			System.String sectionPath = "EFS/EventLog/" + EFS.EventLog.Config.Log.ConfigSectionNameFormat + destination + "/Filter";

			// read section 
			EFS.Common.Config.Reader conf = EFS.Common.Config.Reader.TryCreateReader(sectionPath, EFS.Common.Verify.String(EFS.EventLog.Config.Log.ConfigSectionNameFormat));

			// check
			if (null != conf)
			{
				// set
				Info = conf.GetAttributeBoolean("LogInfo");
				Warn = conf.GetAttributeBoolean("LogWarn");
				Err = conf.GetAttributeBoolean("LogErr");
				Except = conf.GetAttributeBoolean("LogExcept");
				Extended = conf.GetAttributeBoolean("LogExtended");
				Counter = conf.GetAttributeBoolean("LogCounter");
			}
		}
	}

	/// <summary>
	/// Performance log
	/// </summary>
	public class PerformanceLog
	{
		/// <summary>
		/// Filter
		/// </summary>
		private System.Collections.Generic.List<System.String> filter = new System.Collections.Generic.List<System.String>(); 

		/// <summary>
		/// Root Dir
		/// </summary>
		public System.String RootDirectory { get; private set; }

		/// <summary>
		/// Check if marker is in filter
		/// </summary>
		/// <param name="performanceMarkerName"></param>
		/// <returns></returns>
		public System.Boolean Check(System.String performanceMarkerName)
		{
			return filter.Contains(performanceMarkerName);
		}

		/// <summary>
		/// Constructor
		/// </summary>
		public PerformanceLog()
		{
		}

		/// <summary>
		/// load config from web.config
		/// </summary>
		internal void LoadConfig()
		{
			// make path
			System.String sectionPath = "EFS/EventLog/" + EFS.EventLog.Config.Log.ConfigSectionNameFormat + "Performance/Config";

			// read section 
			EFS.Common.Config.Reader conf = EFS.Common.Config.Reader.TryCreateReader(sectionPath, EFS.Common.Verify.String(EFS.EventLog.Config.Log.ConfigSectionNameFormat));

			// check
			if (null != conf)
			{
				// set
				RootDirectory = conf.GetAttributeString("RootDirectory");
				
				// create directory
				System.IO.Directory.CreateDirectory(RootDirectory);

				System.String strFilter = conf.GetAttributeString("Filter");
				if (EFS.Common.Verify.String(strFilter))
				{
					filter = new System.Collections.Generic.List< System.String>(strFilter.Split(new System.Char[] { ';' }, System.StringSplitOptions.RemoveEmptyEntries));
				}
			}
		}
	}

	/// <summary>
	/// Email log
	/// </summary>
	public class EmailLog
	{
		public EFS.EventLog.Config.EventsFilter Filter { get; private set; }
		public System.String EmailAddressRecipient { get; private set; }
		public System.String EmailAddressSender { get; private set; }
		public System.String EmailServer { get; private set; }

		/// <summary>
		/// Constructor
		/// </summary>
		public EmailLog()
		{
			// new filter
			Filter = new EventsFilter();
		}


		/// <summary>
		/// load config from web.config
		/// </summary>
		internal void LoadConfig()
		{
			// make path
			System.String sectionPath = "EFS/EventLog/" + EFS.EventLog.Config.Log.ConfigSectionNameFormat + "Email/Config";

			// read section 
			EFS.Common.Config.Reader conf = EFS.Common.Config.Reader.TryCreateReader(sectionPath, EFS.Common.Verify.String(EFS.EventLog.Config.Log.ConfigSectionNameFormat));

			// check
			if (null != conf)
			{
				// set
				EmailAddressRecipient = conf.GetAttributeString("EmailAddressRecipient");
				EmailAddressSender = conf.GetAttributeString("EmailAddressSender");
				EmailServer = conf.GetAttributeString("EmailServer");

				// load filter
				Filter.LoadConfig("Email");
			}
		}
	}

	/// <summary>
	/// File log
	/// </summary>
	public class FileLog
	{
		public EFS.EventLog.Config.EventsFilter Filter { get; private set; }
		public System.String FilePath { get; private set; }

		/// <summary>
		/// Constructor
		/// </summary>
		public FileLog()
		{
			// new filter
			Filter = new EventsFilter();
		}

		/// <summary>
		/// load config from web.config
		/// </summary>
		internal void LoadConfig()
		{
			// make path
			System.String sectionPath = "EFS/EventLog/" + EFS.EventLog.Config.Log.ConfigSectionNameFormat + "File/Config";

			// read section 
			EFS.Common.Config.Reader conf = EFS.Common.Config.Reader.TryCreateReader(sectionPath, EFS.Common.Verify.String(EFS.EventLog.Config.Log.ConfigSectionNameFormat));

			// check6
			if (null != conf)
			{
				// set
				FilePath = conf.GetAttributeString("Path");

				// load filter
				Filter.LoadConfig("File");
			}
		}
	}

	/// <summary>
	/// Console log
	/// </summary>
	public class ConsoleLog
	{
		public EFS.EventLog.Config.EventsFilter Filter { get; private set; }

		/// <summary>
		/// Constructor
		/// </summary>
		public ConsoleLog()
		{
			// new filter
			Filter = new EventsFilter();
		}

		/// <summary>
		/// load config from web.config
		/// </summary>
		internal void LoadConfig()
		{
			// make path
			System.String sectionPath = "EFS/EventLog/" + EFS.EventLog.Config.Log.ConfigSectionNameFormat + "Console/Config";

			// read section 
			EFS.Common.Config.Reader conf = EFS.Common.Config.Reader.TryCreateReader(sectionPath, EFS.Common.Verify.String(EFS.EventLog.Config.Log.ConfigSectionNameFormat));

			// check
			if (null != conf)
			{
				// load filter
				Filter.LoadConfig("Console");
			}
		}
	}

	/// <summary>
	/// Log config
	/// </summary>
	public class Log
	{
		/// <summary>
		/// Secion name used as section group
		/// </summary>
		private static System.String configSectionName;

		/// <summary>
		/// Constructor
		/// </summary>
		static Log()
		{
			// defualt section
			configSectionName = "";

			// reload config from web.config
			InitConfig();
		}

		/// <summary>
		/// Config section name, from machine.config
		/// </summary>
		public static System.String ConfigSectionName
		{
			get
			{
				return configSectionName;
			}
			set
			{
				// set config file name
				configSectionName = value;

				// reload config from machine config
				InitConfig();
			}
		}

		/// <summary>
		/// Config section name format for use in load config
		/// </summary>
		internal static System.String ConfigSectionNameFormat
		{
			get
			{
				return (EFS.Common.Verify.String(ConfigSectionName)) ? ConfigSectionName + "/" : "";
			}
		}

		/// <summary>
		/// File Config
		/// </summary>
		public static EFS.EventLog.Config.FileLog FileLog { get; private set; }

		/// <summary>
		/// Performance Config
		/// </summary>
		public static EFS.EventLog.Config.PerformanceLog PerformanceLog { get; private set; }

		/// <summary>
		/// Email Config
		/// </summary>
		public static EFS.EventLog.Config.EmailLog EmailLog { get; private set; }

		/// <summary>
		/// Console Config
		/// </summary>
		public static EFS.EventLog.Config.ConsoleLog ConsoleLog { get; private set; }

		/// <summary>
		/// Line separator
		/// </summary>
		public static System.String Separator { get { return "--------------------------------------------"; } }

		#region LoadConfiguration

		/// <summary>
		/// Init log config from web.config
		/// </summary>
		/// <param name="configFilePath"></param>
		private static void InitConfig()
		{
			// reset
			ResetConfig();

			// load
			FileLog.LoadConfig();
			EmailLog.LoadConfig();
			PerformanceLog.LoadConfig();
			ConsoleLog.LoadConfig();

			// make sure directory exists
			if (EFS.Common.Verify.String(FileLog.FilePath))
			{
				// make sure dir exists
				System.IO.Directory.CreateDirectory(FileLog.FilePath);
			}
		}

		/// <summary>
		/// Reset
		/// </summary>
		private static void ResetConfig()
		{
			FileLog = new EFS.EventLog.Config.FileLog();
			ConsoleLog = new EFS.EventLog.Config.ConsoleLog();
			EmailLog = new EFS.EventLog.Config.EmailLog();
			PerformanceLog = new EFS.EventLog.Config.PerformanceLog();
		}

		// Get flag from config
		internal static System.Boolean GetFlag(string value)
		{
			return (value.ToLower() == "false") ? false : true;
		}

		#endregion
	}
}